package cn.yujiago.shiro.service.impl;

import cn.yujiago.shiro.domain.UserDO;
import cn.yujiago.shiro.mapper.UserMapper;
import cn.yujiago.shiro.pojo.LayuiTableResult;
import cn.yujiago.shiro.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private PageHelper pageHelper;

    @Override
    public UserDO getUser(String username, String password) {

        Example example = new Example(UserDO.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("username", username);
        criteria.andEqualTo("password", password);

        return userMapper.selectOneByExample(example);
    }

    @Override
    public UserDO getUser(String username) {
        Example example = new Example(UserDO.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("username", username);

        return userMapper.selectOneByExample(example);
    }

    @Override
    public LayuiTableResult<UserDO> getUserByDeptId(Long deptId, int page, int limit) {

        // 1、设置分页信息，包括当前页数和每页显示的总计数
        pageHelper.startPage(page, limit);

        List<UserDO> userDOS = null;
        if(deptId != null && deptId != 0){
            Example example = new Example(UserDO.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("deptId", deptId);

            userDOS = userMapper.selectByExample(example);
        }
        else{
            userDOS = userMapper.selectAll();
        }

        // 3、获取分页查询后的数据
        PageInfo<UserDO> pageInfo = new PageInfo<>(userDOS);

        LayuiTableResult<UserDO> result = new LayuiTableResult<>();
        result.setCode(0);
        result.setMsg("查询成功！");
        result.setData(userDOS);
        result.setCount(pageInfo.getSize());

        return result;
    }


}
